use crate::base::{dto::SysUser};
use crate::enums::cache_key::CacheKey::{SYSTEM_USER};
use crate::utils::security_utils;
use dyn_fmt::AsStrFormatExt;
use serde_json::from_str;
use crate::utils::cache_utils::{CacheType, GlobalCache};


/**
 * 根据token获取用户信息
 */
pub async fn get_user(token:&str)->Option<SysUser> {
    let id = match security_utils::aes_decrypt(token) {
        Ok(id) => id,
        Err(_) => return None,
    };
    //获取缓存 校验用户是否过期 可接入redis添加过期功能
    let key = SYSTEM_USER.key().format(&[&id]);
    let val = match GlobalCache::get(&key) {
        Some(v) => v,
        None => return None
    };
    let user:SysUser = from_str(&val).unwrap();
    Some(user)
}